package com.cvs.contorller;
import com.cvs.bean.*;
import com.cvs.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;


@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private LevelService levelService;
    @Autowired
    private SignService signService;
    @Autowired
    private JiFenService jiFenService;
    @Autowired
    private ActivityService activityService;
    @RequestMapping("/home")
    public  String home(int uid, Model model){
          List<Level> levels=levelService.findAll();
          String levelName="";
          User user=userService.findOne(uid);
        for (Level le:levels) {
            if (user.getJingyan()>=le.getMoney()) {
                levelName= le.getName();
            }
        }
        model.addAttribute("levelName",levelName);
        model.addAttribute("user",user);
        model.addAttribute("levels",levels);
        return "home";
    }
    @RequestMapping("/sign")
    public  String sign(int uid, Model model){//调转页面
      model.addAttribute("uid",uid);
       List<Activity> activities=activityService.findAll();
        model.addAttribute("activityies",activities);
        return "sign";
    }
    @PostMapping("/actlog")
    public @ResponseBody
    Map actlog(int uid){
        Map map=new HashMap();
        map.put("status",200);
        map.put("msg","查询成功");
        map.put("data",jiFenService.findCount(uid));
        return map;
    }

    @PostMapping("/sign")
    public @ResponseBody String sign1(int uid, Model model){//提交签到请求
        //Date
        Date date= new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd");
        String day=simpleDateFormat.format(date);//日期格式化
        System.out.println(day);
        Sign sign=new Sign();
        sign.setCreatetime(date);
        sign.setDay(Integer.parseInt(day));
        sign.setUid(uid);
        int count=signService.addSign(sign);
        System.out.println(count);
        return count+"";
    }
    @PostMapping("/dayList")
    public @ResponseBody Map dayList(int uid){
        List<Sign> signs=signService.signDay(uid);
        System.out.println(signs);
        Map map=new HashMap();
        map.put("status",200);
        map.put("msg","查询成功");
        map.put("data",signService.signDay(uid));
        return map;
    }
    @RequestMapping("/dayCount")
    public @ResponseBody Map dayCount(int uid){
       /* List<Sign> signs=signService.signDay(uid);
        System.out.println(signs);*/
        Map map=new HashMap();
        map.put("status",200);
        map.put("msg","查询成功");
        map.put("data",signService.signCount(uid));
        return map;
    }
    @PostMapping("/lqJiFen")
    public @ResponseBody Map lqJiFen(int uid,int actid){
        //1.actid根据id获取活动具体内容
        //2.需要查询活动详情信息
        Activity activity=activityService.findOne(actid);
        User user=userService.findOne(uid);//查询用户原来积分的数量
        user.setJifen(user.getJifen()+activity.getMoney());
        int flag=userService.updateUser(user);//更新用户信息
        //增加积分记录
        JiFen jiFen=new JiFen();
        jiFen.setActivity(activity);
        jiFen.setMoney(activity.getMoney());
        jiFen.setUid(uid);
        jiFen.setCreatetime(new Date());
        jiFenService.addJiFenLog(jiFen);

        Map map=new HashMap();
        if (flag==1){
            map.put("status",200);
            map.put("msg","领取成功！");
            map.put("data",null);
        }else {
            map.put("status",0);
            map.put("msg","领取失败！");
            map.put("data",null);
        }
        return map;
    }
    @RequestMapping("/jifenlog")
    public  String jifenlog(int uid, Model model){//调转页面
        model.addAttribute("uid",uid);
        model.addAttribute("list",jiFenService.findAll(uid));
        return "jifenlog";
    }
}